package io.mft.plugin;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;

import io.mft.commons.ioc.ApiBeanName;
import io.mft.commons.ioc.BaseRegistry;
import io.mft.commons.ioc.MFTSpringRegistry;
import io.mft.commons.ioc.SpringIocRegistry;

/**
 * 
 * @类型名称：FunctionSpringRegistry
 * @说明：TODO
 * @创建者: 胡常建
 * @创建时间: 2018年3月7日 下午11:54:33
 * @修改者: 胡常建
 * @修改时间: 2018年3月7日 下午11:54:33
 */
@SpringIocRegistry
public class PluginApplicationSpringRegistry extends BaseRegistry{
	
	static final Map<String, Map<String, ApiBeanName>> PluginApplicationCache=new HashMap<String, Map<String,ApiBeanName>>();
	public PluginApplicationSpringRegistry(String sysNo, String basePackage) {
		super(sysNo, basePackage, PAApplication.class);
	}
	@Override
	protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
		boolean status=beanDefinition.getMetadata().isConcrete();
		return status;
	}
	protected   List<ApiBeanName> parseBeanName(String annotationName,String sysNo,BeanDefinition definition){
		List<ApiBeanName> list=new ArrayList<ApiBeanName>();
		ApiBeanName api_beanName=new ApiBeanName();
		api_beanName.setBeanName(sysNo+"."+definition.getBeanClassName());
		api_beanName.setNote(definition.getBeanClassName());
		api_beanName.setQualifiedName(definition.getBeanClassName());
		api_beanName.setApiInterface(true);
		api_beanName.setAnnotationType(annotationName);
		api_beanName.setBeanClassName(definition.getBeanClassName());
		api_beanName.setSysNo(sysNo);
		list.add(api_beanName);
		Map<String, ApiBeanName> m=PluginApplicationCache.get(sysNo);
		if (m==null) {
			m=new HashMap<>();
			PluginApplicationCache.put(sysNo, m);
		}
		m.put(definition.getBeanClassName(), api_beanName);
		return list;
	}
	
	public static void close(String pluginId) {
		Map<String, ApiBeanName> m=PluginApplicationCache.get(pluginId);
		m.values().forEach(api->{
			MFTSpringRegistry.getApiIdCache().remove(api.getBeanName());
		});
	}
}
